using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace CPRMulti.Tools
{
    public class FindGameObject
    {
        public static int number;
        /// <summary>
        /// 通过子对象的名称来查找子对象（深度递归）
        /// </summary>
        /// <param name="parent">父对象</param>
        /// <param name="childName">子对象名称</param>
        /// <returns></returns>
        public static GameObject GetChileByName(GameObject parent, string childName)
        {
            if (parent.name == childName)
                return parent;

            if (parent.transform.childCount < 1)
                return null;

            GameObject child = null;
            for (int i = 0; i < parent.transform.childCount; i++)
            {
                GameObject gameObject = parent.transform.GetChild(i).gameObject;
                child = GetChileByName(gameObject, childName);

                if (child != null) break;
            }

            return child;
        }

        /// <summary>
        /// 通过名称查找父对象
        /// </summary>
        /// <param name="child">子对象</param>
        /// <param name="parentName">父对象名称</param>
        /// <returns></returns>
        public static GameObject GetParentByName(GameObject child, string parentName)
        {
            if (child.name == parentName)
                return child;

            if (child.transform.parent == null)
                return null;

            GameObject parent = null;
            GameObject gameObject = child.transform.parent.gameObject;
            if (gameObject == null) return null;
            number++;
            parent = GetParentByName(gameObject, parentName);

            return parent;
            
        }

    }
}
